<?php
/**
 * Australian football extension for Joomleague
 * @version $Id$
 * @package jlg_AustralianFootball
 * @copyright Copyright (C) 2007 Julien Vonthron. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * this is free software and parts of it may contain or be derived from the
 * GNU General Public License or other free or open source software licenses.
 * 
 * contact: julien.vonthron[at]gmail.com 
 *   
 * Joomleague is a League Management and Prediction Game for Joomla!
 * Homepage: http://www.joomleague.de 
 *  
 */

class AF_Ranking extends Ranking{
	 
  /**
   * updates teams data according to match result in ranking object table
   * For this specific extension, bonus points are used for behinds points   
   *
   * @param array $data array of ranking objects indexed by team ids
   * @param int $mode mode for home/away/home and away ranking
   * @param object $match data of the game
   * @param boolean $shownegpoints
   */
  function collectPoints(& $data, $mode, $match, $shownegpoints) {  
    $homeId = $match->matchpart1;
    $awayId = $match->matchpart2;
    $decision = $match->decision;
    
    print_r($match);
    if ($decision == 0)
    {
      // normal result      
      // bonus points fields are used for "behind" points
      if ( isset($match->home_score) )
      {
        $home_score=$match->home_score;
        $away_score=$match->away_score;
      }
      else if ( isset($match->matchpart1_result_split) )
      {
        // sum the score per period. must respect format <goals>.<behind>
        
        $periodscore = explode( ';', $match->matchpart1_result_split );
        foreach ($periodscore as $score)
        {
          if ( ereg( "([0-9]+)\.([0-9])+", $score, $res ) ) {
            $home_score += 6*$res[1]+$res[2];
          }
        }
        $periodscore = explode( ';', $match->matchpart2_result_split );
        foreach ($periodscore as $score)
        {
          if ( ereg( "([0-9]+)\.([0-9])+", $score, $res ) ) {
            $away_score += 6*$res[1]+$res[2];
          }
        }
      }
    }
    else 
    {
      // modified result
      $home_score=$match->home_score_decision;
      $away_score=$match->away_score_decision;
    }
    $leg1=$match->l1;
    $leg2=$match->l2;
    
    if ($mode == 0 || $mode == 1) {
      $home = & $data[$homeId];
    }
    else 
    {
      $home = new stdClass(); //in that case, $data wont be affected
      $this->init_stdclass($home); //and now they are also initialized in php4
    }
    if ($mode == 0 || $mode == 2) {
      $away = & $data[$awayId];
    }
    else 
    {
      $away = new stdClass(); //in that case, $data wont be affected
      $this->init_stdclass($away); //and now they are also initialized in php4
    }
    if (!isset($data[$homeId]->headtohead[$awayId])) 
    {
      $home->headtohead[$awayId]=0;
      $home->headtohead_diff[$awayId]=0;
      $home->headtohead_goal_for[$awayId]=0;
      $home->headtohead_awaygoals[$awayId]=0;
    }
    if (!isset($data[$awayId]->headtohead[$homeId])) 
    {
      $away->headtohead[$homeId]=0;
      $away->headtohead_diff[$homeId]=0;
      $away->headtohead_goal_for[$homeId]=0;
      $away->headtohead_awaygoals[$homeId]=0;
    }
    
    $home->cnt_matches++;
    $away->cnt_matches++;
    
    $resultType = ($this->project->allow_add_time) ? $match->match_result_type : 0;    
    switch($resultType)
    {
      case 1: $arr = explode(",",$this->project->points_after_add_time);break;
      case 2: $arr = explode(",",$this->project->points_after_penalty);break;
      default: $arr = explode(",",$this->project->points_after_regular_time);break;
    }
    
    $win_points = $arr[0];
    $draw_points = $arr[1];
    $loss_points = $arr[2];
    
    if ($decision!=1 || ( $decision==1 && (isset($home_score) || isset($away_score)))) 
    {
      if( $home_score > $away_score ) {
        $home->cnt_won++;
        $home->cnt_won_home++;
        $home->sum_points += $win_points; //home_score can't be null...
        
        $away->cnt_lost++;
        $away->sum_points += ( $decision == 0 || isset($away_score) ? $loss_points : 0);
        
        if ( $shownegpoints==1) 
        {
          $home->neg_points += $loss_points;
          $away->neg_points += ( $decision == 0 || isset($away_score) ? $win_points : 0);
        }
        $home->headtohead[$awayId]++; 
        $away->headtohead[$homeId]--;
      }
      else if ( $home_score == $away_score ){
        $home->cnt_draw++;
        $home->cnt_draw_home++;
        $home->sum_points += ( $decision == 0 || isset($home_score) ? $draw_points : 0);
        
        $away->cnt_draw++;
        $away->sum_points += ( $decision == 0 || isset($away_score) ? $draw_points : 0);
        
        if ($shownegpoints==1) 
        {
          $home->neg_points += ( $decision == 0 || isset($home_score) ? $draw_points : 0);
          $away->neg_points += ( $decision == 0 || isset($away_score) ? $draw_points : 0);
        }
      }
      else if ( $home_score < $away_score )
      {
          $home->cnt_lost++;
          $home->cnt_lost_home++;
          $home->sum_points += ( $decision == 0 || isset($home_score) ? $loss_points : 0);
          
          $away->cnt_won++;
          $away->sum_points += $win_points;
          
          if ( $shownegpoints==1) 
          {
            $home->neg_points += ( $decision == 0 || isset($home_score) ? $win_points : 0);
            $away->neg_points += $loss_points;
          }
          $home->headtohead[$awayId]--; 
          $away->headtohead[$homeId]++;
      }
      
      /* bonus points fields are used for behind points, we don't add them to ranking
       points... */
      //$home->sum_points += $match->home_bonus;
      //$home->bonus_points += $match->home_bonus;
      
      //$away->sum_points += $match->away_bonus;
      //$away->bonus_points += $match->away_bonus;
      
      /* goals for/against/diff */
      $home->sum_matchpart1_result += $home_score;
      $home->sum_matchpart2_result += $away_score;
      $home->diff_matchpart_results= $home->sum_matchpart1_result - $home->sum_matchpart2_result;
      $home->sum_matchpart1_legs += $leg1;
      $home->sum_matchpart2_legs += $leg2;
      $home->diff_matchpart_legs= $home->sum_matchpart1_legs - $home->sum_matchpart2_legs;
      
      $away->sum_matchpart1_result += $away_score;
      $away->sum_matchpart2_result += $home_score;
      $away->diff_matchpart_results= $away->sum_matchpart1_result - $away->sum_matchpart2_result;
      $away->sum_matchpart1_legs += $leg2;
      $away->sum_matchpart2_legs += $leg1;
      $away->diff_matchpart_legs= $away->sum_matchpart1_legs - $away->sum_matchpart2_legs;
      
      /* head to head goal diff */
      $home->headtohead_diff[$awayId]+=($home_score-$away_score);
      $away->headtohead_diff[$homeId]-=($home_score-$away_score); 
      
      /*head to head goals for/away goals*/
      $home->headtohead_goal_for[$awayId]+=$home_score;
      $away->headtohead_goal_for[$homeId]+=$away_score;
      $away->headtohead_awaygoals[$homeId]+=$away_score;     
    } 
    else 
    { //none of the 2 teams gets any points
      if ($shownegpoints==1) 
      {
        $home->neg_points += $loss_points;
        $away->neg_points += $loss_points;
      }
      $home->cnt_lost++;
      $away->cnt_lost++;
    }
  }
 
}

?>
